package two.chapter_1.c1_3;

import edu.princeton.cs.algs4.StdOut;


public class Ex1_3_37 {

    public static void main(String[] args) {

        josephus(7,2);
    }

    /**
     * n个人报数，报到m的人出局，指导剩下最后一个人
     * 用队列实现：
     * 队列前的m-1出列后再依次加入到队列尾部
     * 队列中的第m个就是最先出局的人
     * @param n
     * @param m
     * @return
     */
    private static void josephus(int n, int m) {
        Queue<Integer> queue = new LinkedQueue<>();
        for (int i = 0; i <n; i++) {
            queue.enqueue(i);
        }
        while (!queue.isEmpty()) {

            for (int i=0;i<m-1;i++){
                queue.enqueue(queue.dequeue());
            }
            StdOut.print(queue.dequeue());
        }


    }


    private static class Person{

        private int index;
        private int val;

        public Person(int index, int val) {
            this.index = index;
            this.val = val;
        }

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public int getVal() {
            return val;
        }

        public void setVal(int val) {
            this.val = val;
        }


    }


}
